﻿using System;
using System.Collections.Generic;
using System.Text;
using TTClassLibrary;
using TTServer.Messages;
using libCommonClass;
using TTServer.Controller;
using libAsymmetricEncryption;
using libUtilities;

namespace TTServer.MessageProcessor
{
    public class RSAPublicKeyClientMessageProcessor : IMessageProcessor
    {
        public bool Process(libCommonClass.TTMessage message, libInterface.IClientInfoProvider clientInfoProvider)
        {
            if (message is RSAPublicKeyClientMessage)
            {
                RSAPublicKeyClientMessage msg = (RSAPublicKeyClientMessage)message;

                byte[] modulus = msg.modulus;
                byte[] exponent = msg.exponent;

                RSA_Key clientPublicKey = new RSA_Key(modulus, exponent);

                clientInfoProvider.ClientPublicKey = clientPublicKey;

                RSA_KeyManager keyManager = RSA_KeyManager.getInstance();

                RSA_Key serverPublicKey = keyManager.ServerPublicKey;

                RSAPublicKeyServerMessage rsaPKServerMessage =
                    new RSAPublicKeyServerMessage(serverPublicKey.get_modulus(), serverPublicKey.get_exponent());

                clientInfoProvider.AddToMessageQueue(rsaPKServerMessage);

                return true;
            }

            return false;
        }
    }
}
